package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: tensquare-project
 * @description:
 **/
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    public void save(Label label) {
        //主键类型是String 主键通过id生成器生成
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }

    public void update(Label label) {
        labelDao.save(label);
    }

    public void deleteById(String labelId) {
        labelDao.deleteById(labelId);
    }

    public Label findById(String labelId) {
        return labelDao.findById(labelId).get();
    }

    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public List<Label> search(Map map) {
        return labelDao.findAll(getSpecification(map));
    }

    public Page<Label> search(Map map, Pageable pageable) {
        return labelDao.findAll(getSpecification(map), pageable);

    }

    //跟Map构建查询方法需要参数Specification

    /**
     * 通过java代码设置最终发出查询sql语句
     * @param map
     * @return
     */
    public Specification<Label> getSpecification(Map map){
        return new Specification<Label>() {
            //Predicate 返回一个或者多个查询条件
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                //根据标签名称模糊查询 from t_label where lablename like '%?%'
                if(map!=null){
                    String labelname = (String) map.get("labelname");
                    if(StringUtils.isNotBlank(labelname)){
                        Predicate p1 = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + labelname + "%");
                        list.add(p1);
                    }

                    //sql+= state = ?
                    String state = (String) map.get("state");
                    if(StringUtils.isNotBlank(state)){
                        Predicate p2 = criteriaBuilder.equal(root.get("state").as(String.class), state);
                        list.add(p2);
                    }


                    if(list.size()==0){
                        return null;
                    }
                    //查询条件使用and 或者 or拼接
//                    Predicate[] predicates = new Predicate[list.size()];
                    //将list转为数组
//                    predicates = list.toArray(predicates);
                    return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
                }
                return null;
            }
        };
    }
}
